package com.staff.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.common.util.AJAXResultMessage;
import com.common.util.AuthenticationUtil;
import com.common.util.ExtjsPageable;
import com.staff.entity.Staff;
import com.staff.entity.dto.StaffQueryDTO;
import com.staff.service.IStaffService;
import com.syslog.aop.SystemServiceLog;
import com.system.entity.Role;
import com.system.service.IRoleService;

@Controller
@RequestMapping("/staff")
public class StaffController {
	@Autowired
	private IStaffService staffService;
	@Autowired
	private IRoleService roleService;
	
	@SystemServiceLog("新建或修改员工基本信息")
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody AJAXResultMessage saveOrUpdate(StaffQueryDTO staffdto) 
	{
		try {
			
			return staffService.saveOrUpdate(staffdto);
		} catch (Exception e) {
			e.printStackTrace();
			return new AJAXResultMessage(false,"操作失败");
		}
	}
	
	@SystemServiceLog("删除员工基本信息")
	@RequestMapping("/delete")
	public @ResponseBody AJAXResultMessage delete(@RequestParam Long id) 
	{
		try {
			Staff Staff =  staffService.findOne(id);
			if(null!=Staff) {
				staffService.delete(Staff);
			}
			return new AJAXResultMessage(true,"操作成功！");
		} catch (Exception e) {
			return new AJAXResultMessage(false,"操作失败！");
		}
	}
	@SystemServiceLog("批量删除员工基本信息")
	@RequestMapping("/deleteStaffs")
	public @ResponseBody AJAXResultMessage deleteStaffs(@RequestParam Long[] ids) 
	{
		try {
			List<Long> idLists = Arrays.asList(ids);
			if(null!=idLists) {
				staffService.delete(idLists);
			}
			return new AJAXResultMessage(true,"操作成功！");
		} catch (Exception e) {
			return new AJAXResultMessage(false,"操作失败！");
		}
	}
	@SystemServiceLog("查找员工")
	@RequestMapping("/findOne")
	public @ResponseBody Staff findOne(@RequestParam Long id) 
	{
		Staff Staff =  staffService.findOne(id);
		return Staff;
	}
	
	@SystemServiceLog("查找所有员工")
	@RequestMapping("/findAll")
	public @ResponseBody List<Staff> findAll() 
	{
		 List<Staff> lists =  staffService.findAll();
		
		return lists;
	}
	//
	//分页查询（需要DTO）
	@SystemServiceLog("分页查找所有员工")
	@RequestMapping("/findPage")
	public @ResponseBody Page<StaffQueryDTO> findPage(StaffQueryDTO userQueryDto,ExtjsPageable pageable){
		return staffService.findAll(StaffQueryDTO.getSpecification(userQueryDto), pageable.getPageable());
	}
	@SystemServiceLog("登陆身份认证")
	@RequestMapping("/login")
	public void login(Staff staff, HttpServletRequest request, HttpSession session, HttpServletResponse response) 
	{
		try {
			Staff staff1=staffService.findStaff(staff.getStaffNum(), staff.getPassword());
			if(staff1==null)
			{
				response.sendRedirect("../login.jsp");
			}
			session.setAttribute("staffId",staff1.getId());
			session.setAttribute("staffName",staff1.getStaffName());			
			session.setAttribute("staffNum",staff1.getStaffNum());
			//身份认证
			AuthenticationUtil.authenticationLogin(staff);
			response.sendRedirect("../index.jsp");
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "login";
	}
	@RequestMapping("/toIndex")
	public String toIndex() {
		return "index";
	}
	
}
